package com.hezuti.common.utils;

import java.util.List;

import org.apache.commons.collections.ListUtils;
import org.apache.commons.collections.functors.StringValueTransformer;
import org.apache.commons.lang.StringUtils;

/**
 * 集合工具类
 * 
 * @author xiaoyu.dou
 * 
 */
public class ListUtil {
	/**
	 * 按固定大小分割List
	 * 
	 * @param source
	 *            要分割的List
	 * @param size
	 *            分割的大小
	 * @return 分割后的List数组,数组的最后一个小于或等于分割大小
	 */
	@SuppressWarnings("unchecked")
	public static <E> List<E>[] splitListBySize(List<E> source, int size) {
		int pageSize = (int) Math.ceil(source.size() * 1.0 / size);
		List[] subList = new List[pageSize];
		for (int i = 0; i < pageSize; i++) {
			int start = size * i;
			int end = Math.min(source.size(), start + size);

			subList[i] = source.subList(start, end);
		}
		return subList;
	}

	/**
	 * 插入
	 * 
	 * @param list
	 * @param separator
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static String join(List list, String separator) {
		List l = ListUtils.transformedList(list, StringValueTransformer.getInstance());
		return StringUtils.join(l.iterator(), separator);
	}

	/**
	 * 插入
	 * @param list
	 * @param separator
	 * @return
	 */
	public static String join(String[] list, String separator) {
		return StringUtils.join(list, separator);
	}
}
